#ifndef UI_FUNCS_H
#define UI_FUNCS_H

#include <Arduino.h>
#include <lvgl.h>
#include "./ui/ui.h"
#include "WiFi.h"
#include "dev/networkmanager.h"
#include "shared_values.h"
#include "remoteAPI.h"
void func_initalize_ui(){
    if (wifi_initalize_from_file()){
        lv_textarea_set_text(ui_TextScreenIP, WiFi.localIP().toString().c_str());
        lv_textarea_set_text(ui_TextArea7, "Online");
        lv_textarea_set_text(ui_TextAreaSsidInput, global_ssid);
        lv_textarea_set_text(ui_TextAreaPasswdInput, global_password);
    } else {
        lv_textarea_set_text(ui_TextArea7, "Offline");
    }
}

void func_press_btn_pushed(lv_event_t * e)
{
	// Your code here
}

void func_release_btn_pushed(lv_event_t * e)
{
	// Your code here
}

void func_led_btn_pushed(lv_event_t * e)
{
	// Your code here
    toggle_light();
}

void func_moved_btn_pushed(lv_event_t * e)
{
	// Your code here
    set_remote_speed(lv_slider_get_value(ui_Slider1));
    start_motor();

}

void func_moved_btn_released(lv_event_t * e)
{
	// Your code here
    // set_remote_speed(6400);
    stop_motor();
}

void func_speed_slider_changed(lv_event_t * e)
{
	// Your code here
}

void func_Motor_con_wifi_pushed(lv_event_t * e)
{
	// Your code here
}

void func_wifi_connect_pushed(lv_event_t * e)
{
    WiFi.begin(lv_textarea_get_text(ui_TextAreaSsidInput), lv_textarea_get_text(ui_TextAreaPasswdInput));    // 连接WiFi网络
    Serial.printf("正在连接WiFi %s \n", lv_textarea_get_text(ui_TextAreaSsidInput));
    unsigned long startAttemptTime = millis(); // 记录开始尝试连接的时间
  
    while (millis() - startAttemptTime < 5000) { // 等待WiFi连接成功或超时
      if (WiFi.status() != WL_CONNECTED){
        Serial.println("正在连接WiFi...");
        delay(100);
      } else {
        Serial.println("WiFi连接成功");
        lv_textarea_set_text(ui_TextScreenIP, WiFi.localIP().toString().c_str());
        lv_textarea_set_text(ui_TextArea7, "Online");
        wifi_save_to_file(lv_textarea_get_text(ui_TextAreaSsidInput), lv_textarea_get_text(ui_TextAreaPasswdInput));
        return;
      }
    }
    Serial.println("连接WiFi超时失败");
    lv_textarea_set_text(ui_TextArea7, "Offline");
    lv_timer_handler();
}


#endif // UI_FUNCS_H